package springProvider.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.hardy.statics.exception.LogicException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.util.RandomUtil;
import hardy.grpc.helper.server.target.spring.RpcProvider;
import test.common.inface.OpeHumman;
import test.common.model.Humman;
@RpcProvider
@Service
@Transactional
public class HummanImplement implements OpeHumman{
	private List<Humman> list = new ArrayList<>();
	@Override
	public void createHumman() {
		Humman h = new Humman();
		h.setAddr("测试地址："+RandomUtil.randomString(3));
		h.setAge(new Long(System.currentTimeMillis()%100).intValue());
		h.setEmail("email:"+RandomUtil.randomString(3)+"@"+RandomUtil.randomString(2));
		h.setName("name:"+RandomUtil.randomString(6));
		list.add(h); //System.err.println("create list size=="+list.size());
	}

	@Override
	public void updateHumman(Humman humman) {
		int index = list.indexOf(humman);
		if(index<0) throw new LogicException("not found humman:"+humman.getName());
		list.remove(index);
	}

	@Override
	public List<Humman> listHumman(Humman humman) {
		if(humman==null) {
			//System.out.println("listHumman list size===>"+list.size());
			return list;
		}
		else if(humman.getName()==null) throw new LogicException("name 不能为空");
		else {//System.out.println("filte list size===>"+list.size());
			return list.stream().filter(h->{return h.getName().equalsIgnoreCase(humman.getName());}).collect(Collectors.toList());
		}
	}

}
